package 每日一题;

import java.util.HashMap;
import java.util.Map;

public class 数组中出现次数超过一半的数字 {
    public static int MoreThanHalfNum_Solution(int [] array) {
        int num=array.length/2;
        System.out.println(num);
        Map<Integer,Integer> map=new HashMap<>();

        for (int x:array) {
            map.put(x,map.getOrDefault(x,0)+1);
        }

        int res=0;
        for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
            int count=entry.getValue();
            if (count>num) {
                res= entry.getKey();
            }
        }
        return res;

    }

    public static void main(String[] args) {
        int[] arr={3,3,3,3,2,2,2};
        System.out.println(MoreThanHalfNum_Solution(arr));
    }
}
